package com.cms.server.resolver.plugins.scsq;

import java.sql.ResultSet;
import java.util.Date;

import com.efounder.builder.base.data.DataSetUtils;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.eai.data.JParamObject;
import com.efounder.form.EFFormDataModel;
import com.efounder.form.io.FormDataResolverAdapter;
import com.efounder.form.server.resolver.util.SYS_MDL_VAL;
import com.efounder.sql.JStatement;
import com.metadata.bizmodel.SYS_MODEL;
import com.persistence.FLOW_TASK_LIST;

import dwz.common.util.DateUtil;

public class CMSSubmitCheckCCTZSL extends FormDataResolverAdapter {

	public void prepareSaveForm(JStatement stmt, EFFormDataModel model, JParamObject PO) throws Exception {		
		String                            strSql = "";
		EFDataSet                     formHeadDS = null;
		EFDataSet                     formItemDS = null;
		EFDataSet                    syslDataset = null;
		EFRowSet                      formHeadRS = EFRowSet.getInstance();
		EFDataSet                         viewDS = null;
		EFRowSet                          viewRS = EFRowSet.getInstance();
		ResultSet                             rs = null;
		EFDataSet                            eds = model.getBillDataSet();
		EFRowSet                             ers = eds.getRowSet(0);
	    String                            F_KJQJ = ers.getString(PO.GetValueByParamName(SYS_MODEL._BLFL_KJQJ_COL_, SYS_MDL_VAL.BILL_KJQJ), "");
	    String                            F_GUID = ers.getString(PO.GetValueByParamName(SYS_MODEL._BILL_GUID_COL_, SYS_MDL_VAL.BILL_GUID), "");
	    String                            F_DJBH = ers.getString(PO.GetValueByParamName(SYS_MODEL._BILL_BH_COL_, SYS_MDL_VAL.BILL_BH), null);
	    String                          errorMSG = "";
	    double                              scsl = 0.0;
		double                              ccsl = 0.0;
		String[]                        primeKey = null;
		
	    if(!PO.GetValueByParamName("FLOW_ID", "").equals("CCTZ_FLOW")) return;

		formItemDS = ers.getDataSet("CCTZMX");
		
		//获取生产通知单剩余数量
		strSql = "select SCTZMX.F_XMBH,SCTZMX.F_CPBH," 
			   + " round(sum(SCTZMX.F_SCSL), 2) as F_SCSL, SCSYSL_V.F_CCSL as F_CCSL, 0 as F_CCSL "
			   + " from SCTZMX"
			   + " LEFT JOIN SCSYSL_V ON SCTZMX.F_XMBH = SCSYSL_V.F_XMBH and SCTZMX.F_CPBH = SCSYSL_V.F_CPBH and SCSYSL_V.F_HTBH = '" + ers.getString("F_HTBH", "") + "'"
			   + " where exists(select 1 from SCTZXX where SCTZXX.F_DJBH = SCTZMX.F_DJBH and SCTZXX.F_KJQJ = SCTZMX.F_KJQJ and " 
			   + " SCTZXX.F_GUID = SCTZMX.F_GUID  and SCTZXX.F_HTBH = '" + ers.getString("F_HTBH", "") 
			   + "' and SCTZXX.F_XMBH = '" + ers.getString("F_XMBH", "") + "' and "
			   + " SCTZXX.F_DJZT = '1')"
			   + " and exists(select 1 from FLOW_TASK_END_LIST where FLOW_TASK_END_LIST.FLOW_ID = 'SCTZ_FLOW' " 
			   + " and FLOW_TASK_END_LIST.BIZ_GUID = SCTZMX.F_GUID)"
			   + " group by SCTZMX.F_XMBH,SCTZMX.F_CPBH"
			   + " order by SCTZMX.F_XMBH,SCTZMX.F_CPBH";
		rs = stmt.executeQuery(strSql);
		syslDataset = DataSetUtils.resultSet2DataSet(rs, syslDataset);
		syslDataset.setPrimeKey(new String[]{"F_XMBH","F_CPBH"});
		syslDataset.buildPrimeKeyIndex();

		for(int i = 0; i < formItemDS.getRowCount(); i++) {
			primeKey = new String[]{formItemDS.getRowSet(i).getString("F_XMBH", ""), formItemDS.getRowSet(i).getString("F_CPBH", "")};
			viewRS = (EFRowSet) syslDataset.getRowSet(primeKey);
			if(viewRS != null) {
				scsl = viewRS.getNumber("F_SCSL", 0.0).doubleValue();
				ccsl = viewRS.getNumber("F_CCSL", 0.0).doubleValue();
				if(formItemDS.getRowSet(i).getNumber("F_CCSL", 0.0).doubleValue() > (scsl - ccsl)) {
					errorMSG = "<br>项目[" + formItemDS.getRowSet(i).getString("F_XMMC", "") + "]-产品[" 
					         + formItemDS.getRowSet(i).getString("F_CPMC", "") + "]出厂数量大于剩余数量，<br>出厂数量：" + formItemDS.getRowSet(i).getNumber("F_CCSL", 0.0).doubleValue()
					         + "<br>剩余数量：" + (scsl - ccsl) + "请重新打开单据维护出厂数量！";
					throw new Exception(errorMSG);
				}
			}
		}
	}
}